using System;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;

using WHC.Pager.Entity;
using Microsoft.Practices.EnterpriseLibrary.Data;
using WHC.Hotel.Entity;
using WHC.Hotel.IDAL;
using WHC.OrderWater.Commons;

namespace WHC.Hotel.DAL
{
    /// <summary>
    /// TeaGoods 的摘要说明。
    /// </summary>
    public class KTVGoods : BaseDAL<KTVGoodsInfo>, IKTVGoods
    {
        #region 对象实例及构造函数

        public static KTVGoods Instance
        {
            get
            {
                return new KTVGoods();
            }
        }
        public KTVGoods()
            : base("YL_KTVGoods", "ID")
        {
        }

        #endregion

        /// <summary>
        /// 将DataReader的属性值转化为实体类的属性值，返回实体类
        /// </summary>
        /// <param name="dr">有效的DataReader对象</param>
        /// <returns>实体类对象</returns>
        protected override KTVGoodsInfo DataReaderToEntity(IDataReader dataReader)
        {
            KTVGoodsInfo teaGoodsInfo = new KTVGoodsInfo();
            SmartDataReader reader = new SmartDataReader(dataReader);

            teaGoodsInfo.ID = reader.GetInt32("ID");
            teaGoodsInfo.ItemNo = reader.GetString("ItemNo");
            teaGoodsInfo.Name = reader.GetString("Name");
            teaGoodsInfo.Price = reader.GetDecimal("Price");
            teaGoodsInfo.GoodsType = reader.GetString("GoodsType");
            teaGoodsInfo.Flag = reader.GetBoolean("Flag");
            teaGoodsInfo.UnitName = reader.GetString("UnitName");
            teaGoodsInfo.UnitCost = reader.GetDecimal("UnitCost");
            teaGoodsInfo.StockQuantity = reader.GetInt32("StockQuantity");
            teaGoodsInfo.AlarmQuantity = reader.GetInt32("AlarmQuantity");
            teaGoodsInfo.Note = reader.GetString("Note");
            teaGoodsInfo.DuralDays = reader.GetDouble("DuralDays");

            return teaGoodsInfo;
        }

        /// <summary>
        /// 将实体对象的属性值转化为Hashtable对应的键值
        /// </summary>
        /// <param name="obj">有效的实体对象</param>
        /// <returns>包含键值映射的Hashtable</returns>
        protected override Hashtable GetHashByEntity(KTVGoodsInfo obj)
        {
            KTVGoodsInfo info = obj as KTVGoodsInfo;
            Hashtable hash = new Hashtable();

            hash.Add("ItemNo", info.ItemNo);
            hash.Add("Name", info.Name);
            hash.Add("Price", info.Price);
            hash.Add("GoodsType", info.GoodsType);
            hash.Add("Flag", info.Flag);
            hash.Add("UnitName", info.UnitName);
            hash.Add("UnitCost", info.UnitCost);
            hash.Add("StockQuantity", info.StockQuantity);
            hash.Add("AlarmQuantity", info.AlarmQuantity);
            hash.Add("Note", info.Note);
            hash.Add("DuralDays", info.DuralDays);

            return hash;
        }
                       
        /// <summary>
        /// 根据商品类型获取该类型的商品列表
        /// </summary>
        /// <param name="goodsType">商品类型</param>
        /// <returns></returns>
        public List<KTVGoodsInfo> FindByGoodsType(string goodsType)
        {
            string sql = string.Format("GoodsType like '%{0}%' or dbo.f_GetPy(GoodsType) like '{0}%' ", goodsType);
            return this.Find(sql);
        }

        /// <summary>
        /// 根据商品名称获取商品列表
        /// </summary>
        /// <param name="goodsType">商品类型</param>
        /// <returns></returns>
        public List<KTVGoodsInfo> FindByName(string goodsName)
        {
            string sql = string.Format("Name like '%{0}%' or dbo.f_GetPy(Name) like '{0}%' ", goodsName);
            return this.Find(sql);
        }

        /// <summary>
        /// 增加库存
        /// </summary>
        /// <param name="quantity"></param>
        /// <returns></returns>
        public bool AddStockQuantiy(string ItemNo, string itemName, int quantity, DbTransaction trans)
        {
            string sql = string.Format("Update {0} set StockQuantity=StockQuantity+{1} where ItemNo='{2}' and Name='{3}' ",
                this.tableName, quantity, ItemNo, itemName);
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand command = db.GetSqlStringCommand(sql);
            bool result = false;
            if (trans != null)
            {
                result = db.ExecuteNonQuery(command, trans) > 0;
            }
            else
            {
                result = db.ExecuteNonQuery(command) > 0;
            }

            return result;
        }

        /// <summary>
        /// 增加库存
        /// </summary>
        /// <param name="quantity"></param>
        /// <returns></returns>
        public bool AddStockQuantiy(string ItemNo, string itemName, int quantity)
        {
            return this.AddStockQuantiy(ItemNo, itemName, quantity, null);
        }
    }
}